package com.weng.coder.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.weng.coder.common.utils.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import io.swagger.annotations.Api;

/**
 * 公用模块 Web API
 *
 * @作者 wengzhonghui
 * @创建时间 2018-02-25 15:49:52
 */
@RestController("coderCommonController")
@RequestMapping("/common") 
@Api(value = "公用模块相关接口")
public class CommonController{
	
	@ResponseBody
    @RequestMapping(value = "/download-file", method = RequestMethod.GET)
    public void download(String filePath, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
        if(StringUtils.isEmpty(filePath)){
        	return;
        }
    	File file = new File(filePath);
    	if(!file.exists()){
    		return;
    	}
    	byte[] data = FileUtil.fileToBytes(file);
        response.setContentType("application/x-msdownload; charset=UTF-8");
        response.setContentLength(data.length);
        response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(data);
        outputStream.flush();
        outputStream.close();
    }
}

